package animator.phantom.gui.preview;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.controller.*;
import animator.phantom.gui.timeline.TimeLineDisplayPanel;
import animator.phantom.gui.GUIResources;
import com.jhlabs.image.CheckFilter;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.awt.Dimension;
import animator.phantom.gui.*;
import java.io.File;
import java.awt.geom.Point2D;

//--- Preview display panel.
public class PreViewPanel extends JPanel
{
	private BufferedImage frame = null;
	private float scale;
	//--- 
	private Dimension scaledScreensize;
	private Dimension screenSize;
	private Point2D.Float origo = new Point2D.Float( 0,0 );
	//---
	private BufferedImage noPreview;
	private BufferedImage noPreviewScaled;
	private boolean fullRepaint = true;
	//---
	private int panelWidth;
	private int panelHeight;
	//---
	private int prevWHalf;
	private int prevHHalf;

	public PreViewPanel()
	{
		panelWidth = Toolkit.getDefaultToolkit().getScreenSize().width
				- Application.SMALL_WINDOW_WIDTH
		- 		- AnimFrameGUIParams.VIEW_EDITOR_WIDTH_PAD;
		panelHeight = AnimFrameGUIParams.VIEW_EDITOR_HEIGHT;
		Dimension d = new Dimension( panelWidth, panelHeight );
		setPreferredSize( d );
		setMaximumSize( d );
		setMinimumSize( d );

		screenSize = ProjectController.getScreenSize();
		
		noPreview = GUIResources.getResourceBufferedImage( GUIResources.noPreview );
		prevWHalf = noPreview.getWidth() / 2;
		prevHHalf = noPreview.getHeight() / 2;
		noPreviewScaled = noPreview;

		setScale( 1.0f );
	}

	public void setFrame( BufferedImage frame_ )
	{
		frame = frame_;
	}

	public void setScale( float newScale )
	{
		this.scale = newScale;
		scaledScreensize = new Dimension( (int)(screenSize.width  *  scale), (int)(screenSize.height  * scale) );
		
		createNoPreviewImage( scaledScreensize.width, scaledScreensize.height );

		int origoX = ( panelWidth - scaledScreensize.width ) / 2;
		int origoY = ( panelHeight - scaledScreensize.height ) / 2;
		origo = new Point2D.Float( (float) origoX, (float) origoY );
	}

	private void createNoPreviewImage( int screenWidth, int screenHeight )
	{
		//--- Create smaller image if needed
		if( screenWidth < noPreview.getWidth() ||
			screenHeight < noPreview.getHeight() )
		{
			//--- Select scale 
			float wt = (float)screenWidth / noPreview.getWidth();
			float ht = (float)screenHeight / noPreview.getHeight();

			float tscale;
			if( wt < ht ) tscale = wt;
			else tscale = ht;
			int nw = (int)(tscale * noPreview.getWidth());
			int nh = (int)(tscale * noPreview.getHeight());

			Image scaled = noPreview.getScaledInstance( nw, nh, Image.SCALE_FAST );
			noPreviewScaled = new BufferedImage( nw, nh, BufferedImage.TYPE_INT_ARGB );
			Graphics g = noPreviewScaled.getGraphics();
			g.drawImage( scaled, 0, 0, null );
		}
		else
		{
			noPreviewScaled = noPreview;//scaled version will garbage collect

		}
	}

	//-------------------------------------------------------------- PAINT
	public void setFullRepaint( boolean value ){ fullRepaint = value; }

	public void paintComponent(Graphics g)
	{
		if( fullRepaint )
		{
			g.setColor(  GUIColors.previewBgColor );
			g.fillRect( 0, 0, panelWidth, panelHeight);
		}
		
		int x = (int)Math.round(origo.x);
		int y = (int)Math.round(origo.y);

		if( frame == null )
		{
			g.setColor( Color.black );
			g.fillRect( x, y, scaledScreensize.width, scaledScreensize.height );
			g.drawImage( noPreviewScaled,
					x + ((int)(scaledScreensize.width)/2) - prevWHalf,
					y + ((int)(scaledScreensize.height)/2) - prevHHalf, null );
			return;
		}

		g.drawImage( frame, x, y, null );
		g.drawRect( x, y, (int)(scaledScreensize.width), (int)(scaledScreensize.height) );
	}

}//end class
